/**
*
* This viewer was partly adapted from ORB-SLAM2!
*
* Their disclaimer:
*
* This file is part of ORB-SLAM2.
*
* Copyright (C) 2014-2016 Raúl Mur-Artal <raulmur at unizar dot es> (University of Zaragoza)
* For more information see <https://github.com/raulmur/ORB_SLAM2>
*
* ORB-SLAM2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ORB-SLAM2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ORB-SLAM2. If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef VIEWER_H
#define VIEWER_H
#include "MapDrawer.h"
#include <mutex>
#include "PointCloudBuffered.h"
#include "../utils/Logging.h"
#include <unistd.h>
namespace  REVOGui
{
class MapDrawer;
class System;
using namespace std;

class Viewer
{
public:

    Viewer(std::shared_ptr<MapDrawer> pMapDrawer);

    // Main thread function. Draw points, keyframes, the current camera pose and the last processed
    // frame. Drawing is refreshed according to the camera fps. We use Pangolin.
    void Run();

    void RequestFinish();

    void RequestStop();

    bool isFinished();

    bool isStopped();

    void Release();

    inline bool quitRequest() const
    {
        return quitRequested;
    }

    void DrawPclBuffered()
    {
        std::unique_lock<std::mutex> lock(this->mMtxPclBuffered);
        for (uint i = 0; i < this->mPclKfsBuffered.size();++i)
        {
            mPclKfsBuffered[i]->drawPoints(mpMapDrawer->vpKfsF.at(i).cast<float>());
        }
    }
private:
    std::vector<Eigen::Matrix4f, Eigen::aligned_allocator<Eigen::Matrix4f>> vpKfsF;
    bool Stop();
    std::mutex mPclMutex;
    std::shared_ptr<MapDrawer> mpMapDrawer;

    // 1/fps in ms
    double mT;
    float mImageWidth, mImageHeight;
    float mViewpointX, mViewpointY, mViewpointZ, mViewpointF;
    bool mSaveModel;
    bool CheckFinish();
    void SetFinish();
    bool mbFinishRequested;
    bool mbFinished;
    std::mutex mMutexFinish;
    bool mbStopped;
    bool mbStopRequested;
    bool quitRequested;
    std::mutex mMutexStop;
    bool mNeedsUpdate; //only when new keyframe arrives
    std::vector<std::shared_ptr<PointCloudBuffered> > mPclKfsBuffered;
    std::mutex mMtxPclBuffered;
};
}
#endif // VIEWER_H
	

